/**
 * Copyright (C), 2018 store
 * File Name: ChannelContextHolder.java
 * Encoding: UTF-8
 * Date: 18-9-13 上午9:17
 * History:
 */
package com.swak.frame.context;

import com.swak.frame.util.GetterUtil;

/**
 * FileName: ChannelContextHolder.java
 *
 * @author ColleyMa
 * @version 18-9-13 上午9:17
 */
public class ApiContextHolder {
    private static ThreadLocal<ApiContext> threadVar = new ThreadLocal<ApiContext>();

    public static ApiContext get() {
        return threadVar.get();
    }

    public static void set(ApiContext context) {
        threadVar.set(context);
    }

    public static void clear() {
        threadVar.set(null);
        threadVar.remove();
    }

    public static Integer getChannelId() {
        ApiContext context = get();

        if (context != null) {
            return GetterUtil.getInteger(context.getChannelId());
        }

        return null;
    }

    public static String getBizName() {
        ApiContext context = get();
        if (context != null) {
            return context.getBizName();
        }
        return null;
    }

    public static String getAppId() {
        ApiContext context = get();

        if (context != null) {
            return GetterUtil.getString(context.getAppId(), "-1");
        }

        return "-1";
    }

    public static String getChannelToken() {
        ApiContext context = get();

        if (context != null) {
            return context.getChannelToken();
        }

        return null;
    }
}
